package cn.com.netedge.iot.frontend.biz.mq;

import cn.com.netedge.iot.frontend.biz.protocol.AbstractHandle;
import cn.com.netedge.iot.frontend.biz.protocol.ProtocolHandleFactory;
import cn.com.netedge.iot.frontend.biz.service.DeviceService;
import com.alibaba.fastjson2.JSON;
import cn.com.netedge.iot.common.constants.MqConstants;
import cn.com.netedge.iot.common.dataobject.DeviceInfo;
import cn.com.netedge.iot.common.dataobject.ReceiveFrame;
import cn.com.netedge.iot.common.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * 接收报文解析
 * Created by Administrator on 2018-09-11.
 */
@Component
@Scope("prototype")
public class ReceiveMessageConsumer extends BaseReceiveMessageConsumer {
    private static Logger logger = LoggerFactory.getLogger(ReceiveMessageConsumer.class);

    @Autowired
    private ProtocolHandleFactory protocolHandleFactory;
    @Autowired
    private DeviceService deviceService;

    @Override
    public void execute(String msg) {
        if (MqConstants.QUEUE_UP_MESSAGE_NORMAL.equals(queueName)) {
            onNormalMessage(msg);
        } else if (MqConstants.QUEUE_UP_MESSAGE_EMERGENCY.equals(queueName)) {
            onHighPriorityMessage(msg);
        } else {
            logger.debug("msg not support.");
        }
    }

    public void onNormalMessage(String message) {
        logger.info("Queue:{} 收到消息:{}", MqConstants.QUEUE_UP_MESSAGE_NORMAL, message);
        ReceiveFrame frame = JSON.parseObject(message, ReceiveFrame.class);
        String deviceNumber = frame.getDeviceNumber();
        int deviceType = frame.getDeviceType();
        if(deviceType < 0) {
            logger.debug("deviceType < 0, ignore it.");
            return;
        }
        if(StringUtil.isEmpty(deviceNumber)) {
            logger.debug("deviceNumber is empty, ignore it.");
        }
        DeviceInfo deviceInfo = deviceService.getDeviceInfo(deviceNumber, deviceType);
        if(deviceInfo == null) {
            logger.info("deviceNumber:{} deviceType:{} 's deviceInfo is null, ignore it.", deviceNumber, deviceType);
            return;
        }
        Short protocol = deviceInfo.getProtocol();
        AbstractHandle dealHandle = protocolHandleFactory.createHandle(protocol);
        if (dealHandle == null) {
            logger.error("protocol:{} 's handler is null");
            return;
        }
        processMessage(dealHandle, frame);
    }

    @Async("asyncServiceExecutor")
    public void onHighPriorityMessage(String message) {
        logger.info("Queue:{} 收到消息:{}", MqConstants.QUEUE_UP_MESSAGE_EMERGENCY, message);
        ReceiveFrame frame = JSON.parseObject(message, ReceiveFrame.class);
        String deviceNumber = frame.getDeviceNumber();
        int deviceType = frame.getDeviceType();
        if(deviceType < 0) {
            logger.debug("deviceType < 0, ignore it.");
            return;
        }
        if(StringUtil.isEmpty(deviceNumber)) {
            logger.debug("deviceNumber is empty, ignore it.");
        }
        DeviceInfo deviceInfo = deviceService.getDeviceInfo(deviceNumber, deviceType);
        if(deviceInfo == null) {
            logger.info("deviceNumber:{} deviceType:{} 's deviceInfo is null, ignore it.", deviceNumber, deviceType);
            return;
        }
        Short protocol = deviceInfo.getProtocol();
        AbstractHandle dealHandle = protocolHandleFactory.createHandle(protocol);
        if (dealHandle == null) {
            logger.error("protocol:{} 's handler is null");
            return;
        }
        processMessage(dealHandle, frame);
    }
}
